<?php

	$q = $_GET['id_resto'];
	$type_graph = $_GET['type'];
	
	if (!$q) return;
	
	set_include_path('../140dev/db');
	
	require_once('140dev_config.php');	  
	require_once('db_lib.php');
	
	$oDB = new db;
    
 	if(!$oDB)
  	{
  		die('Could not connect: ' . mysql_error());
  	}
	
	try{
		
		if($type_graph == "REVIEWS_SCORING"){ 
			$result = $oDB->select("SELECT AVG_SCORE_GLOBAL FROM freshbuzzing_1.HIST_SCORE_REVIEWS WHERE ID_RESTAURANT=" . $q . " ORDER BY DT_SCORE DESC LIMIT 1");
			
			$result_arr = array();
			while($row = mysqli_fetch_row($result))
			{
			/*
				$datetime = strtotime($row[2]);
				$dt_score = date("Y-m-d", $datetime);
				$scoring = $row[1];
				$web_review = $row[0];
				
				$return_search = $return_search . $web_review . ";" . $dt_score . ";" . $scoring . "|";
				*/
				
				array_push($result_arr,$row[0]);
			}
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
		}
		if($type_graph == "TWITTER_FOLLOWERS"){
			$sql = "SELECT DATE_FORMAT(t3.DT_TIMESTAMP, '%Y-%m-%d'), ROUND(AVG(t3.NUM_FOLLOWERS)) FROM MAST_RESTAURANTS t1 ";
			$sql = $sql . "INNER JOIN MAST_TWITTER_ACCOUNTS t2 ON t1.DS_REST_TWITTER = t2.DS_SCREEN_NAME ";
			$sql = $sql . "INNER JOIN HIST_TWITTER_ACCOUNTS t3 ON t2.DS_SCREEN_NAME = t3.ID_USER_TW ";
			$sql = $sql . "WHERE t1.ID_RESTAURANT = '" . $q . "' GROUP BY DATE_FORMAT(t3.DT_TIMESTAMP, '%Y-%m-%d') ";
			$sql = $sql . "ORDER BY DT_TIMESTAMP ASC ";
			
			$result = $oDB->select($sql);
			
			$return_search = "";
			while($row = mysqli_fetch_row($result))
			{
				$datetime = strtotime($row[0]);
				$dt_update = date("Y-m-d", $datetime);
				$followers = $row[1];
				
				$return_search = $return_search . $dt_update . ";" . $followers . "|";
			}
			
			$return_search = substr_replace($return_search ,"",-1);
			
			echo $return_search;
		}
		if($type_graph == "FACEBOOK_LIKES"){

			$sql = "SELECT DATE_FORMAT(t3.TIMESTAMP, '%Y-%m-%d'), ROUND(AVG(t3.LIKES)) FROM MAST_RESTAURANTS t1 ";
			$sql = $sql . "INNER JOIN MAST_FACEBOOK_ACCOUNTS t2 ON t1.DS_REST_FACEBOOK = t2.USER_FB ";
			$sql = $sql . "INNER JOIN HIST_FACEBOOK_ACCOUNTS t3 ON t2.USER_FB = t3.USER_FB ";
			$sql = $sql . "WHERE t1.ID_RESTAURANT = '" . $q . "' GROUP BY DATE_FORMAT(t3.TIMESTAMP, '%Y-%m-%d') ";
			$sql = $sql . "ORDER BY TIMESTAMP ASC ";
			
			$result = $oDB->select($sql);
			
			$return_search = "";
			while($row = mysqli_fetch_row($result))
			{
				$datetime = strtotime($row[0]);
				$dt_update = date("Y-m-d", $datetime);
				$likes = $row[1];
				
				$return_search = $return_search . $dt_update . ";" . $likes . "|";
			}
			
			$return_search = substr_replace($return_search ,"",-1);
			
			echo $return_search;
		}
		
	
	



	}
	catch(Exception $e){
		echo $e;
	}	
	



?>